<?php

Class AttitudeResult extends CI_Model {
    
    function get_list($limit) {
        
        
        $this->db->select('*');
        $this->db->from('attitude_result');
        
        if ($this->input->post('search_type')) {
            $search_type = $this->input->post('search_type');
            $search = $this->input->post('search');
            $this->db->like($search_type, $search);
        }
        //$this->db->limit($this->config->item('number_of_rows'), $limit);
        $this->db->order_by("id", "asc");
        $query = $this->db->get();

        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return false;
        }
    }
    
    function get($id) {
        $query = $this->db->get_where('attitude_result', array('id' => $id));
        return $query->row_array();
    }
    
    function insert($newAttitudeResult) {
        if ($this->db->insert('attitude_result', $newAttitudeResult)) {
            return "Đã thêm!";
        } else {
            return "Unable to add";
        }
    }
    
    function update($attitudeResult)
    {
        $this->db->where('id', $attitudeResult['id']);
        $this->db->update('attitude_result', $attitudeResult);
        return "Đã cập nhật!";
    }
    
    function delete($id) {
        $this->db->delete('attitude_result', array('id' => $id));
        return "Đã xóa!";
        
    }
    
    function get_last() {
        //$query = $this->db->get_where('attitude_result', array('id' => $id));
        $this->db->select('*');
        
        
        $this->db->from('attitude_result');
        $this->db->order_by("id", "desc");
        $this->db->limit('1');
        $query = $this->db->get();
        return $query->row_array();
    }
    
    

    function user_by_group() {
        $institute_id = $this->session->userdata('institute_id');
        $user_group = array();
        $user_group[] = array('Group Name', 'Registered users');
        $query = $this->db->query("select * from user_group where institute_id = '$institude_id'");
        $result = $query->result_array();
        foreach ($result as $value) {
            $gid = $value['gid'];
            $group_name = $value['group_name'];
            $query = $this->db->query("select * from users where gid='$gid' and institute_id = '$institute_id' ");
            $nou = $query->num_rows();
            $user_group[] = array($group_name, intval($nou));
        }
        return $user_group;
    }

    // update user detail
    function update_user($user_id) {
        $institute_id = $this->session->userdata('institute_id');
        $username = $_POST['username'];
        $name = $_POST['name'];
        $user_email = $_POST['user_email'];
        $user_password = $_POST['user_password'];
        $user_credit = $_POST['user_credit'];
        $user_group = $_POST['user_group'];
        $account_type = $_POST['account_type'];
        $logged_in = $this->session->userdata('logged_in');
        // if loged in user admin
        if ($logged_in['su'] == "1") {
            // if password changed
            if ($user_password != "") {
                $user_detail = array(
                    'username' => $username,
                    'name' => $name,
                    'email' => $user_email,
                    'credit' => $user_credit,
                    'gid' => $user_group,
                    'su' => $account_type,
                    'password' => md5($user_password)
                );
            } else {
                // if password not changed
                $user_detail = array(
                    'username' => $username,
                    'name' => $name,
                    'email' => $user_email,
                    'credit' => $user_credit,
                    'gid' => $user_group,
                    'su' => $account_type
                );
            }
        } else {
            // if loged in user not admin
            // if password changed
            if ($user_password != "") {
                $user_detail = array(
                    'name' => $name,
                    'password' => md5($user_password)
                );
            } else {
                // if password not changed
                $user_detail = array(
                    'name' => $name,
                );
            }
        }

        $this->db->where("institute_id", $institute_id);
        $this->db->where('id', $user_id);
        $this->db->update('users', $user_detail);
        return "User updated";
    }
    
    

}


